package com.tiantianyuegou.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.trouble.framework.utils.MD5;
import com.tiantianyuegou.activity.BaseActivity;
import com.tiantianyuegou.R;
import com.tiantianyuegou.constant.Constant;
import com.tiantianyuegou.http.ApiDefine;
import com.tiantianyuegou.http.SPHelper;
import com.tiantianyuegou.util.LoadingDialog;
import com.tiantianyuegou.util.ToastUtilsCenter;
import com.tiantianyuegou.util.Tools;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 重置支付密码
 * Created by ivany on 2016/1/15.
 */
public class ResetPaymentCodeActivity extends BaseActivity implements TextWatcher, View.OnClickListener {

    private EditText mEt_pwd, mEt_confirm_pwd;
    private Button mBtn_sure;

    private LoadingDialog mLoadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_resetpay_pwd);
       Tools.changeSystemBar(this);
        setTitle("重置支付密码");
        initViews();
    }

    @Override
    public void findViewByIds() {
        mEt_confirm_pwd = (EditText) findViewById(R.id.edt_setpaypwd_again);
        mEt_pwd = (EditText) findViewById(R.id.edt_setpaypwd);
        mBtn_sure = (Button) findViewById(R.id.btn_setpaypwd_sure);
    }

    @Override
    public void initListener() {
        mEt_pwd.addTextChangedListener(this);
        mEt_confirm_pwd.addTextChangedListener(this);
        mBtn_sure.setOnClickListener(this);
    }

    @Override
    public void initData() {
        mLoadingDialog = new LoadingDialog(this);

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        String pwd = mEt_pwd.getText().toString();
        String pwd_confirm = mEt_confirm_pwd.getText().toString();
        if (pwd != null && !"".equals(pwd) && pwd_confirm != null && !"".equals(pwd_confirm)) {
            mBtn_sure.setAlpha(1.0f);
            mBtn_sure.setEnabled(true);
        } else {
            mBtn_sure.setAlpha(0.5f);
            mBtn_sure.setEnabled(false);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_setpaypwd_sure:
                mLoadingDialog.setMsg("正在重置支付密码");
                String pwd = mEt_pwd.getText().toString();
                String pwd_confirm = mEt_confirm_pwd.getText().toString();
                if ("".equals(pwd)) {
                    ToastUtilsCenter.makeText(this, "请输入新密码", false).show();
                } else if(pwd.length()<6||pwd.length()>6){
                    ToastUtilsCenter.makeText(this, "请输入6位长度的新密码", false).show();
                }else if ("".equals(pwd_confirm)) {
                    ToastUtilsCenter.makeText(this, "请输入确认密码", false).show();
                } else if(pwd_confirm.length()<6||pwd_confirm.length()>6){
                    ToastUtilsCenter.makeText(this, "请输入6位长度的密码", false).show();
                }else if (!pwd.equals(pwd_confirm)) {
                   ToastUtilsCenter.makeText(this, "两次密码不一致，请重新输入", false).show();
                } else {
                    mLoadingDialog.show();
                    sureReset(pwd);
                }
                break;
        }
    }

    private void sureReset(String password) {
        RequestParams params = new RequestParams();
        params.addBodyParameter("tmId", SPHelper.getTmId());
        params.addBodyParameter("newPassword", password);
        params.addBodyParameter("password", MD5.md5(SPHelper.getPassword()).toLowerCase());
        Log.e("TAG", "request params:" + SPHelper.getTmId() + ":" + password + ":"
                + MD5.md5(SPHelper.getPassword()).toLowerCase() + ":" + SPHelper.getPassword());
        loadData(HttpRequest.HttpMethod.POST, ApiDefine.RESET_PAY_PWD, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                mLoadingDialog.dismiss();
                Log.e("TAG", "reset pay pass :" + responseInfo.result);
                if (TextUtils.isEmpty(responseInfo.result)) {
                    showMsg("重置支付密码失败");
                } else {
                    try {
                        JSONObject root = new JSONObject(responseInfo.result);
                        String code = root.getString("code");
                        if ("100".equals(code)) {
                            Intent intent = new Intent();
                            setResult(Constant.RESET_PAYMENTCODE_RESULT, intent);
                            finish();
                            showMsg("重置支付密码成功");
                        } else {
                            String msg = root.getString("message");
                            showMsg(msg);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        showMsg("解析异常");
                    }
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                mLoadingDialog.dismiss();
                Log.e("TAG", "request failure :" + s);
                showMsg("当前网络不可用，请检查您的网设置");
            }
        });
    }
}
